package com.fastCms.business.frame.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastCms.business.frame.daos.AlbumMapper;
import com.fastCms.business.frame.pojo.PageParam;
import com.fastCms.business.frame.pojo.entity.Album;
import com.fastCms.business.frame.pojo.entity.AlbumContent;
import com.fastCms.business.frame.pojo.po.*;
import com.fastCms.business.frame.pojo.vo.AlbumContentVO;
import com.fastCms.business.frame.pojo.vo.AlbumVO;
import com.fastCms.business.frame.service.AlbumService;
import com.fastCms.business.frame.utils.BeanConvertUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户相册 服务实现类
 * </p>
 *
 * @author quan
 * @since 2024-06-05
 */
@Transactional
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements AlbumService {
    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public PageInfo<AlbumVO> showPage(PageParam<AlbumQueryPO> pageParam) {
        AlbumQueryPO recordPO = pageParam.getQueryFields();
        LambdaQueryWrapper<Album> queryWrapper = new LambdaQueryWrapper<>();
        PageInfo<AlbumVO> result = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize())
            .doSelectPageInfo(() -> {
                    if (StringUtils.isNotBlank(recordPO.getSearchKey())) {
                        queryWrapper.eq(Album::getAlbumName, recordPO.getSearchKey());
                    }
                    albumMapper.selectList(queryWrapper);
                }
            );
        return result;
    }

    @Override
    public Boolean albumSaveOrUpdate(AlbumSaveOrUpdatePO param) {
        String id = param.getId();
        Album album;
        if (StringUtils.isBlank(id)) {
            album = new Album();
            BeanUtil.copyProperties(param, album);
        } else {
            album = getById(id);
            BeanUtil.copyProperties(param, album);
        }
        this.saveOrUpdate(album);
        return true;
    }

    @Override
    public Boolean albumSort(List<SortPO> param) {
        param.stream().forEach(index -> {
            Album album = new Album();
            album.setId(index.getId());
            album.setSort(index.getSort());
            albumMapper.updateById(album);
        });

        return true;
    }

    @Override
    public Boolean albumDelete(String id) {
        int i = albumMapper.deleteById(id);
        return true;
    }


}
